package com.inibb.app.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inibb.app.constant.GenericCodeConstant;
import com.inibb.app.converter.GoodsConvertor;
import com.inibb.app.dao.JpaGenericDao;
import com.inibb.app.dao.api.IDao;
import com.inibb.app.exception.ServiceVLDException;
import com.inibb.app.hibernate.SimpleHibernateTemplate;
import com.inibb.app.pagination.dto.PaginationCriteria;
import com.inibb.app.po.Goods;
import com.inibb.app.po.GoodsPicRef;
import com.inibb.app.po.Pic;
import com.inibb.app.service.IGoodsService;
import com.inibb.app.vo.GoodsSC;
import com.inibb.app.vo.GoodsVO;



@Transactional
@Service
public class GoodsServiceImpl implements IGoodsService{

	private static final long serialVersionUID = 1L;
	
	private SimpleHibernateTemplate<Goods, Integer> dao;
//	private SimpleHibernateTemplate<Pic, Integer> picDao;
//	private SimpleHibernateTemplate<GoodsPicRef, Integer> goodsPicRefDao;
	
	private IDao<Goods, Integer> genericDao;
	private IDao<Pic, Integer> picDao;
	private IDao<GoodsPicRef, Integer> goodsPicRefDao;
	
	@PersistenceContext(unitName = "entityManager")
	private EntityManager entityManager ;
	
	@Autowired
	public void setEntityManagerFactory() {
		picDao = new JpaGenericDao<Pic, Integer>( entityManager,Pic.class);
		goodsPicRefDao = new JpaGenericDao<GoodsPicRef, Integer>( entityManager,GoodsPicRef.class);
		
		genericDao = new JpaGenericDao<Goods, Integer>(entityManager,Goods.class);
		dao = new SimpleHibernateTemplate<Goods, Integer>();
	}

	@Override
	public GoodsVO getGoods(Integer id) {
		return GoodsConvertor.toVO(genericDao.get(id));
	}

	public void deleteGoods(Integer id) {
		genericDao.delete(id);
	}

	/*public Integer countGoodsByType(final Integer goodsTypeId) {
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("goodsTypeId", goodsTypeId);
		return genericDao.queryNativeSqlRowCount("admin", "countGoodsByType", criteria);
	}

	@SuppressWarnings("unchecked")
	public List<GoodsVO> searchGoodsByType(final Integer goodsTypeId,final Integer start) {
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("goodsTypeId", goodsTypeId);
		
		List<Object[]> obj = genericDao.queryNativeSqlForList("admin", "selectGoodsByType", criteria);
		
		List<GoodsVO> goodss = new ArrayList<GoodsVO>();
		
			for(Object[] l:obj){
				GoodsVO g = new GoodsVO();
				if(l[0]!=null)
					g.setGoodsId(Integer.parseInt(l[0].toString()));
				if(l[1]!=null)
					g.setName(l[1].toString());
				if(l[2]!=null)
					g.setDesp(l[2].toString());
				if(l[3]!=null)
					g.setGoodsTypeId(Integer.parseInt(l[3].toString()));
				if(l[4]!=null)
					g.setPrice(l[4].toString());
				if(l[5]!=null)
					g.setBrand(l[5].toString());
				if(l[6]!=null)
					g.setActvInd(l[6].toString());
				
				goodss.add(g);
			}
		return goodss;
		
	}*/
	
	public int countGoods(GoodsSC paginationCriteria) {
		BigInteger total = (BigInteger) genericDao.queryNativeSqlForObject("admin", "countPageGoods", paginationCriteria);
		
		return total.intValue();
	}

	public List<GoodsVO> searchGoods(GoodsSC paginationCriteria) {
		List<Goods> list = (List<Goods>) genericDao.queryJpqlForList("admin", "searchPageGoodsJpql", paginationCriteria,paginationCriteria.getStartRow(),paginationCriteria.getPageSize());
		List<GoodsVO> gcList = new ArrayList<GoodsVO>();
		for(Goods obj:list){
			gcList.add(GoodsConvertor.toVO(obj));
		}
		return gcList;
	}
	
	public int countGoods(PaginationCriteria paginationCriteria) {
		BigInteger total = (BigInteger) genericDao.queryNativeSqlForObject("admin", "countPageGoods", paginationCriteria);
		
		return total.intValue();
	}

	public List<GoodsVO> searchGoods(PaginationCriteria paginationCriteria) {
		List<Goods> list = (List<Goods>) genericDao.queryJpqlForList("admin", "searchPageGoodsJpql", paginationCriteria,paginationCriteria.getRowStart(),paginationCriteria.getPage());
		List<GoodsVO> gcList = new ArrayList<GoodsVO>();
		for(Goods obj:list){
			gcList.add(GoodsConvertor.toVO(obj));
		}
		return gcList;
	}

	@Override
	public Pic savePic(Pic p) {
		return picDao.merge(p);
	}
	
	@Override
	public void saveGoodsPicRef(GoodsPicRef r){
		goodsPicRefDao.merge(r);
	}

	@Override
	public GoodsVO saveGoods(GoodsVO p) throws ServiceVLDException{
		Goods g = null;
		if(p.getGoodsId()!=null){
			Map<String, Object> criteria = new HashMap<String, Object>();
			criteria.put("goodsId", p.getGoodsId());
			
			genericDao.executeNativeSqlUpdate("admin", "deleteGoodsPicRefById", criteria);
			
			g = genericDao.merge(GoodsConvertor.toPO(p));
		}else{
			g = genericDao.persist(GoodsConvertor.toPO(p));
		}
		//Goods g = genericDao.//dao.saveOrUpdate(GoodsConvertor.toPO(p));

		Pic pic = p.getDefaultPic();//new Pic();
		if(pic.getName()!=null){			
//			pic.setName(file.getOriginalFilename());
			pic = savePic(pic);
//			p = GoodsConvertor.toVO(dao.save(GoodsConvertor.toPO(p)));
			GoodsPicRef gpr = new GoodsPicRef();
			gpr.setGoodsId(g.getGoodsId());
			gpr.setPicId(pic.getPicId());
			gpr.setMainInd(GenericCodeConstant.IND_YES);
			saveGoodsPicRef(gpr);
		}
		if(g==null)
			return null;
		else
			return GoodsConvertor.toVO(g);
	}
	
}
